package org.jxc.dao;

import org.hibernate.SessionFactory;
import org.jxc.domain.ProductType;
import org.team4.dao.GenericDAO;
import org.team4.exception.CRUDException;
import org.team4.util.CodeHelper;
import org.team4.vo.Page;

/**
 * <p>程序名称：       DepartmentDAO.java</p>
 * <p>程序说明：       部门DAO</p>
 * <p>版权信息：       Copyright 深圳市维远泰克科技有限公司</p>
 * <p>时间：          Jan 7, 2011 4:03:51 PM</p>	
 * 
 * @author：          Marker.King
 * @version：         Ver 0.1
 */
public class ProductTypeDAO extends GenericDAO<ProductType> {

	private static final long serialVersionUID = 376038639038947152L;
	private static ProductTypeDAO instance;
	
	public static ProductTypeDAO getInstance(SessionFactory sessionFactory) {
        if(instance == null) {
            instance =  new ProductTypeDAO(sessionFactory);
        }
        return instance;
    }
	
	private ProductTypeDAO(SessionFactory sessionFactory) {
		super(sessionFactory, ProductType.class);
	}
	/**
	 * 查找下一个编号
	 * @param superId
	 * @return
	 * @throws CRUDException
	 */
	public String findNextCode(String superId) throws CRUDException {
		Page<ProductType> page = new Page<ProductType>();
		page.setCurrPage(1);
		page.setPageSize(1);
		page = pagination(page, "from ProductType where superId = ?", "code desc", superId);

		String nextCode = "";
		if(page.getList().size() > 0) {
			nextCode = CodeHelper.getNextCode(page.getList().get(0).getCode(), 3);
		} else {
			ProductType p = this.get(superId);
			if(p == null) {
				nextCode = CodeHelper.getFirstCode(null, 3);
			} else {
				nextCode = CodeHelper.getFirstCode(p.getCode(), 3);
			}
		}
		
		return nextCode;
	}
}